<?

/**
 * $Id: Request.class.php 395 2008-05-28 19:41:06Z dakl $
 * Obsluga requestow
 *
 */

class Request {

	
    /**
     * Pobiera wartosc
     *
     * @param string $value
     * @return unknown
     */
    static function Get($value,$xssremover = true){
        if(isset($_REQUEST[$value]))
        {
            if($xssremover == true)
				return self::CleanXss(self::QuotesRemover($_REQUEST[$value]));
            else
				return self::QuotesRemover($_REQUEST[$value]);
        }
    }

    /**
     * Sprawdza czy istnieje
     *
     * @param string $value
     * @return unknown
     */
    static function Check($value){
        if(isset($_REQUEST[$value]) || isset($_POST[$value])) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Sprawdza czy istnieje $_POST
     *
     * @return bool
     */
    static function IsPost() {
        if(isset($_POST) && !empty($_POST)) {
            return true;
        } else {
            return false;
        }
    }

	 /**
     * Sprawdza czy istnieje $_GET
     *
     * @return bool
     */
    static function IsGet() {
        if(isset($_GET) && !empty($_GET)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * pobiera cookie z wykorzystaniem xss remove'ra
     *
     * @param string $value
     * @param bool $xssremover = true - czy wykorzystywac xssremovera
     */
    static function GetCookie($value,$xssremover = true)
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            return self::CleanXss(self::QuotesRemover($_COOKIE[$value]));
            else
            return self::QuotesRemover($_COOKIE[$value]);
        }
    }

    static function SetCookie($name,$value,$time = null,$path = '/')
    {
		if ($time == '') {
			$time = null;
		}

		setcookie($name, $value, $time, $path);
		$_COOKIE[$name] = $value;
    }

    static function GetCookieArray($value,$xssremover = true, $delimiter = '_##_')
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            $raw = self::CleanXss(self::QuotesRemover($_COOKIE[$value]));
            else
            $raw = self::QuotesRemover($_COOKIE[$value]);

	    return explode($delimiter, $raw);
        }
	else return array();
    }

    static function SetCookieArray($name,$array,$time = null, $glue = '_##_')
    {
	$value = implode($glue, $array);
	
	self::SetCookie($name, $value);
    }


    static function GetCookieAssocArray($value,$xssremover = false, $delimiter = '_#_', $delimiterKey = '_%_' )
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            $raw = self::CleanXss(self::QuotesRemover($_COOKIE[$value]));
            else
            $raw = self::QuotesRemover($_COOKIE[$value]);

	    $gluedArray = explode($delimiter, $raw);
	    $array = array();
	    foreach ($gluedArray as $val) {
		$keyValue = explode($delimiterKey, $val) ;
		$array[$keyValue[0]] = $keyValue[1];
	    }
	    
	    return $array;

        }
	else return array();
    }

    static function SetCookieAssocArray($name,$array,$time = null, $glue = '_#_', $glueKey = '_%_')
    {

	$gluedArray = $array();
	foreach ($array as $key => $val) {
	    $gluedArray[] = $key . $glueKey . $val;
	}

	$value = implode($glue, $gluedArray);


	self::SetCookie($name, $value);
    }


    /**
     * pobiera post z wykorzystaniem xss remove'ra
     *
     * @param string|array $value
     * @param bool $xssremover = true - czy wykorzystywac xssremovera
     *
     * @return string|array
     */
    static function GetPost($value,$xssremover = true, $striptags = true)
    {
        if(is_array($value)) {
            if(isset($_POST[$value['name']][$value['key']])) {

                if($striptags == true) {
                    $ret = self::StripTags($_POST[$value['name']][$value['key']]);
                }
                else {
                    $ret = $_POST[$value['name']][$value['key']];
                }
				return Registry::Get('purifier')->purify($ret);
                if($xssremover == true) {
                    return self::CleanXss($ret);
                }
                else {
                    return self::QuotesRemover($ret);
                }
            } else
				return null;
        } else {
            if(isset($_POST[$value])) {

				if ($striptags == true)
					$ret = self::StripTags($_POST[$value]);
				else
					$ret = $_POST[$value];

                if($xssremover == true) 
                    return self::CleanXss(self::QuotesRemover($ret));
                else
                    return self::QuotesRemover($ret);

            } else
				return null;
        }
    }

    /**
     * pobranie calej tablicy POST z wykorzystaniem xss removera
     *
     * @param boolean $xssremover
     */
    static function GetAllPost($xssremover = true) {
        if($xssremover == true) {
            return self::CleanXss(self::QuotesRemover($_POST));
        } else {
            return self::QuotesRemover($_POST);
        }
    }

	/**
     * pobranie calej tablicy POST z wykorzystaniem xss removera
     *
     * @param boolean $xssremover
     */
    static function GetAllGet($xssremover = true) {
        if($xssremover == true) {
            return self::CleanXss(self::QuotesRemover($_GET));
        } else {
            return self::QuotesRemover($_GET);
        }
    }

    /**
     * pobiera get z wykorzystaniem xss remove'ra
     *
     * @param string|array $value
     * @param bool $xssremover = true - czy wykorzystywac xssremovera
     *
     * @return string|array
     */
    static function GetGet($value,$xssremover = true)
    {
        if(isset($_GET[$value]))
        {
            if($xssremover == true) {
                return self::CleanXss(self::QuotesRemover($_GET[$value]));
            } else {
                return self::QuotesRemover($_GET[$value]);
            }
        }
    }




    static function SetPost($variable, $value) {
        $_POST[$variable] = $value;
    }


    /**
     * xss remover
     *
     * @param string|array $value
     * @return string|array
     */
    static function CleanXss($value) {
        if(is_array($value)) {
            foreach($value as $k => $v) {
                $value[$k] = self::CleanXss($v);
            }
            return $value;
        } else {
			
			$purifier = Registry::Get('__purifier__');
			if(!$purifier)
			{
				Core::RequireOnce(Config::Get('PATH_CORE') . '/lib' . DIRECTORY_SEPARATOR . 'htmlpurifier' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'HTMLPurifier.auto.php');
				$config = HTMLPurifier_Config::createDefault();
				Registry::Set('__purifier__', new HTMLPurifier($config));				
				$purifier = Registry::Get('__purifier__');
			}
			$out = htmlspecialchars($purifier->purify($value), ENT_QUOTES);
			$out = str_replace('&amp;', '&', $out);
			return $out;
        }
    }


    /**
     * strip tags
     *
     * @param string|array $value
     * @return string|array
     */
    static function StripTags($value) {
        if(is_array($value)) {
            foreach($value as $k => $v) {
                $value[$k] = self::StripTags($v);
            }
            return $value;
        } else {
            return strip_tags($value);
        }
    }

    /**
     * quotes remover
     *
     * @param string|array $value
     * @return string|array
     */
    static function QuotesRemover($value) {
        if (get_magic_quotes_gpc ()) {
			if (is_array($value)) {
				foreach ($value as $key => $val) {
					$value[$key] = self::QuotesRemover($val);
				}
				return $value;
			} else {
				return stripslashes($value);
			}
		}
		return $value;
    }
    /**
     * Zwraca ip klienta
     * @return string 
     */
    public static function getClientIP() {
        if(isset($_SERVER['REMOTE_ADDR'])) {
            return $_SERVER['REMOTE_ADDR'];
        } else {
            return false;
        }
    }
}
